﻿using STT.Entities;
using STT.Layer.Connection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace STT.Admin.UserControls
{
    public partial class ucCaNhan : System.Web.UI.UserControl
    {
        ToolBox tb = new ToolBox();
        protected void Page_Load(object sender, EventArgs e)         
        {
            //if (!IsPostBack)
                LoadInFoCaNhan();

        }
        private void LoadInFoCaNhan()
        {
            DtlCaNhan.DataSource = tb.GetDataTableById2("TbGiangVien_GetMaGV", Session["TenDN"].ToString().Trim());
            DtlCaNhan.DataBind();
        }

        protected void DtlCaNhan_CancelCommand(object source, DataListCommandEventArgs e)
        {
            DtlCaNhan.EditItemIndex = -1;
            LoadInFoCaNhan();
        }

        protected void DtlCaNhan_DeleteCommand(object source, DataListCommandEventArgs e)
        {

        }
        protected void DtlCaNhan_EditCommand(object source, DataListCommandEventArgs e)
        {
            DtlCaNhan.EditItemIndex = e.Item.ItemIndex;
            LoadInFoCaNhan();
        }

        protected void DtlCaNhan_UpdateCommand(object source, DataListCommandEventArgs e)
        {
            try
            {
                if (e.CommandName.Equals("Update"))
                 {
                    ETbGiangVien hv = new ETbGiangVien();
                    hv.MaGV = DtlCaNhan.DataKeys[e.Item.ItemIndex].ToString();
                    TextBox txthoten = (TextBox)e.Item.FindControl("txtTencanhan");
                    TextBox txtdiachi = (TextBox)e.Item.FindControl("txtdiachi");
                    TextBox txtsdt = (TextBox)e.Item.FindControl("txtsdt");
                    TextBox txtghichu = (TextBox)e.Item.FindControl("txtghichu");
                    TextBox txtanh = (TextBox)e.Item.FindControl("txtanh");
                    FileUpload fileup = (FileUpload)e.Item.FindControl("Fulanh");
                    if (fileup.HasFile)
                    {
                            if (System.IO.File.Exists(Server.MapPath("~/Uploads/Avatar/") + txtanh.Text))
                                System.IO.File.Delete(Server.MapPath("~/Uploads/Avatar/") + txtanh.Text);
                            string id = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + Guid.NewGuid().ToString() + "¿" + fileup.FileName;
                            fileup.SaveAs(Server.MapPath("~/Uploads/Avatar/") + id);
                            hv.Anh = id;
                            hv.MaGV = DtlCaNhan.DataKeys[e.Item.ItemIndex].ToString();
                            hv.TenGV = txthoten.Text;
                            hv.DiaChi = txtdiachi.Text;
                            hv.Sdt = txtsdt.Text;
                            hv.GhiChu = txtghichu.Text;
                            Business.BTbGiangVien.UpdateInfoGV(hv);
                    }
                    else
                    {
                        hv.MaGV = DtlCaNhan.DataKeys[e.Item.ItemIndex].ToString();
                        hv.TenGV = txthoten.Text;
                        hv.DiaChi = txtdiachi.Text;
                        hv.Sdt = txtsdt.Text;
                        hv.GhiChu = txtghichu.Text;
                        hv.Anh = txtanh.Text;
                        Business.BTbGiangVien.UpdateInfoGV(hv);
                    }
                    DtlCaNhan.EditItemIndex = -1;
                    LoadInFoCaNhan();
                    //Response.Redirect("Default.aspx?idpage=cn");
                }
            }
            catch { }
        }

        protected void DtlCaNhan_ItemDataBound(object sender, DataListItemEventArgs e)
        {

        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?idpage=dmk1303");
        }
      
    }
}